package com.excesys.exsecs.om.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.admin.api.dto.UserInfo;
import com.excesys.exsecs.admin.api.entity.SysUser;
import com.excesys.exsecs.admin.api.feign.RemoteUserService;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.om.dto.OrderDto;
import com.excesys.exsecs.om.dto.OrderHistoryDto;
import com.excesys.exsecs.om.entity.OmWOrder;
import com.excesys.exsecs.om.service.OmODictService;
import com.excesys.exsecs.om.service.OmWOrderService;
import com.excesys.exsecs.om.utils.WebSocketSend;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 工单表
 *
 * @author weiyj
 * @date 2023-02-24 13:52:55
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/omworder")
@Tag(name = "工单表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class OmWOrderController {

	private final OmWOrderService omWOrderService;

	private final WebSocketSend webSoketTest;

	private final OmODictService omODictService;

	private final RemoteUserService userService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param omWOrder 工单
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('om_omworder_get')")
	public R getOmWOrderPage(Page page, OmWOrder omWOrder, String flag) {
		return R.ok(omWOrderService.getOrderWithRelPage(page, omWOrder, flag));
	}

	/**
	 * 三维首页分页查询
	 * @param page 分页对象
	 * @param omWOrder 工单
	 * @return
	 */
	@Operation(summary = "三维首页分页查询", description = "三维首页分页查询")
	@GetMapping("/getHomepage")
	@PreAuthorize("@pms.hasPermission('om_omworder_get')")
	public R getHomepage(Page page, OmWOrder omWOrder) {
		return R.ok(omWOrderService.getHomepage(page, omWOrder));
	}

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param omWOrder 工单
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询工单代办")
	@GetMapping("/getPage")
	@PreAuthorize("@pms.hasPermission('om_omworder_get')")
	public R getPage(Page page, OmWOrder omWOrder) {
		return R.ok(omWOrderService.getPage(page, omWOrder));
	}

	/**
	 * 通过typeName自动获取当前新的工单名称
	 * @param typeName
	 * @return R
	 */
	@Operation(summary = "通过typeName自动获取当前新的工单名称", description = "通过typeName自动获取当前新的工单名称")
	@GetMapping("/getCurrentOrderNameSeq/{typeName}")
	@PreAuthorize("@pms.hasPermission('om_omworder_get')")
	public R getCurrentOrderNameSeq(@PathVariable("typeName") String typeName) {
		return R.ok(omWOrderService.getCurrentOrderNameSeq(typeName));
	}

	/**
	 * 通过id查询工单
	 * @param orId id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{orId}")
	@PreAuthorize("@pms.hasPermission('om_omworder_get')")
	public R getById(@PathVariable("orId") Integer orId) {
		return R.ok(omWOrderService.getById(orId));
	}

	/**
	 * 新增工单
	 * @param orderDto 工单
	 * @return R
	 */
	@Operation(summary = "新增工单", description = "新增工单")
	@SysLog("新增工单")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('om_omworder_add')")
	public R save(@RequestBody OrderDto orderDto) {
		if ("2".equals(orderDto.getAppointType())) {
			R<SysUser> userInfo =  userService.getUserById(orderDto.getHandleUserId());
			webSoketTest.onMessage(null, userInfo.getData().getUsername() + "/你有一个新的工单待处理请注意查收!!!");
		}
		return R.ok(omWOrderService.saveOmWOrder(orderDto));
	}

	/**
	 * 修改工单
	 * @param orderDto 工单
	 * @return R
	 */
	@Operation(summary = "修改工单", description = "修改工单")
	@SysLog("修改工单")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('om_omworder_edit')")
	public R updateById(@RequestBody OrderDto orderDto) {

		return R.ok(omWOrderService.updateOmWOrder(orderDto));
	}

	/**
	 * 通过id删除工单
	 * @param orId id
	 * @return R
	 */
	@Operation(summary = "通过id删除工单", description = "通过id删除工单")
	@SysLog("通过id删除工单")
	@DeleteMapping("/{orId}")
	@PreAuthorize("@pms.hasPermission('om_omworder_del')")
	public R removeById(@PathVariable Integer orId) {

		return R.ok(omWOrderService.removeOmWOrderById(orId));
	}

	/**
	 * 操作工单
	 * @param orderHistoryDto 工单
	 * @return R
	 */
	@Operation(summary = "操作工单", description = "操作工单")
	@SysLog("操作工单")
	@PostMapping("/operate")
	@PreAuthorize("@pms.hasPermission('om_omworder_operate')" )
	public R operateOmWOrder(@RequestBody OrderHistoryDto orderHistoryDto) {
		OmWOrder omWOrder = omWOrderService.getByOrderNo(orderHistoryDto.getOrderNo());
		if (ObjectUtil.isNotEmpty(omWOrder)) {
			if ("1".equals(orderHistoryDto.getOrderStatus())) {
				R<SysUser> userInfo = userService.getUserById(orderHistoryDto.getHandleUserId());
				webSoketTest.onMessage(null, userInfo.getData().getUsername() + "/你有一个新的工单待处理请注意查收!!!");
			}
		}
		return R.ok(omWOrderService.operateOmWOrder(orderHistoryDto));
	}

	/**
	 * 新增巡检记录
	 * @param idList 巡检记录
	 * @return R
	 */
	@Operation(summary = "删除多个工单", description = "删除多个工单")
	@SysLog("删除多个工单")
	@DeleteMapping("/removeByIds")
	@PreAuthorize("@pms.hasPermission('om_omworder_del')")
	public R removeByIds(@RequestBody List<Integer> idList) {
		return R.ok(omWOrderService.removeByIds(idList));
	}

	/**
	 * 根据年/月/日查询当年工单记录
	 */
	@Operation(summary = "根据年份查询当年工单记录", description = "根据年份查询当年工单记录")
	@SysLog("根据年份查询当年工单记录")
	@GetMapping("/orderByYear")
	@PreAuthorize("@pms.hasPermission('om_omworder_get')")
	public R getList() {
		Map<String, Map<String, String>> countMap = omWOrderService.getOrderSize();
		//System.out.println("map：" + countMap);
		return R.ok(countMap, "工单首页统计");
	}

	/**
	 * 根据年/月/日查询报修记录
	 */
	@Operation(summary = "根据年份查询当年工单记录", description = "根据年份查询当年工单记录")
	@SysLog("根据年份查询当年工单记录")
	@GetMapping("/orderByYear/getList")
	@PreAuthorize("@pms.hasPermission('om_omworder_get')")
	public R getCountList() {
		return R.ok(omWOrderService.getWarrantyMethod());
	}

	/**
	 * 统计年/去年 月/上个月 天/昨天 的报修次数
	 */
	@GetMapping("/getRepairList")
	@PreAuthorize("@pms.hasPermission('om_omworder_get')")
	public R getRepairList() {
		return R.ok(omWOrderService.getRepairList());
	}

}
